

using System;
using System.Collections.Generic;
using System.Text;
using DataAccessLayer;
using EntityLayer;
using DataLayer;
using System.Data;
using InfraStructureLayer;

namespace BusinessLayer
{
    public class busArticles : BusinessBase
    {
        public busArticles()
            : base()
        {
        }
        #region  Code Factory Generated Methods


        public DataTable SelectArticles()
        {
            try
            {
                datArticles insDatArticles = new datArticles();
                DbConnector insDbConnector = new DbConnector();
                return insDatArticles.SelectArticles(insDbConnector);
            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
        public void SelectArticlesByArticleId(entArticles parEntArticles)
        {
            try
            {
                datArticles insDatArticles = new datArticles();
                DbConnector insDbConnector = new DbConnector();
                insDatArticles.SelectArticlesByArticleId(parEntArticles, insDbConnector);
            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
        }
       
        #endregion
        public void InsertArticles(entArticles parEntArticles)
        {
            DbConnector insDbConnector = new DbConnector();
            try
            {

                insDbConnector.BeginTransaction();
                datArticles insDatArticles = new datArticles();

                insDatArticles.InsertArticles(parEntArticles, insDbConnector);
                datArticleContents insDatArticleContents = new datArticleContents();

                datArticleContentPublishLocations insDatArticleContentPublishLocations = new datArticleContentPublishLocations();

                datArticleArticleCategories insDatArticleArticleCategories = new datArticleArticleCategories();

                foreach (entArticleArticleCategories insEntArticleArticleCategories in parEntArticles.ArticleArticleCategories)
                {
                    insEntArticleArticleCategories.ArticleId = parEntArticles.ArticleId;
                    insDatArticleArticleCategories.InsertArticleArticleCategories(insEntArticleArticleCategories, insDbConnector);
                }

                foreach (entArticleContents insEntArticleContents in parEntArticles.ArticleContentsInsert)
                {
                    insEntArticleContents.ArticleId = parEntArticles.ArticleId;
                    
                    insDatArticleContents.InsertArticleContents(insEntArticleContents, insDbConnector);
                    foreach (entArticleContentPublishLocations insEntArticleContentPublishLocations in insEntArticleContents.ArticleContentPublishLocations)
                    {
                        insEntArticleContentPublishLocations.ArticleContentId = insEntArticleContents.ArticleContentId;
                        insDatArticleContentPublishLocations.InsertArticleContentPublishLocations(insEntArticleContentPublishLocations, insDbConnector);

                    }
                }
                foreach (entArticleContents insEntArticleContents in parEntArticles.ArticleContentsUpdate)
                {
                    insEntArticleContents.ArticleId = parEntArticles.ArticleId;
                    insDatArticleContents.UpdateArticleContentsByArticleContentId(insEntArticleContents, insDbConnector);

                    entArticleContentPublishLocations insEntArticleContentPublishLocationsDel = new entArticleContentPublishLocations();
                    insEntArticleContentPublishLocationsDel.ArticleContentId = insEntArticleContents.ArticleContentId;
                    insDatArticleContentPublishLocations.DeleteArticleContentPublishLocationsByArticleContentId(insEntArticleContentPublishLocationsDel, insDbConnector);
                    foreach (entArticleContentPublishLocations insEntArticleContentPublishLocations in insEntArticleContents.ArticleContentPublishLocations)
                    {
                        insEntArticleContentPublishLocations.ArticleContentId = insEntArticleContents.ArticleContentId;
                        insDatArticleContentPublishLocations.InsertArticleContentPublishLocations(insEntArticleContentPublishLocations, insDbConnector);

                    }
                }
                foreach (entArticleContents insEntArticleContents in parEntArticles.ArticleContentsDelete)
                {
                    entArticleContentPublishLocations insEntArticleContentPublishLocations = new entArticleContentPublishLocations();
                    insEntArticleContentPublishLocations.ArticleContentId = insEntArticleContents.ArticleContentId;
                    insDatArticleContentPublishLocations.DeleteArticleContentPublishLocationsByArticleContentId(insEntArticleContentPublishLocations, insDbConnector);
                    insDatArticleContents.DeleteArticleContentsByArticleContentId(insEntArticleContents, insDbConnector);
                }
                insDbConnector.CommitTransaction();
            }
            catch (SFException ex)
            {
                insDbConnector.RollbackTransaction();
                this.ErrorMessage = ex.Message;
            }
            catch (Exception ex)
            {
                insDbConnector.RollbackTransaction();
                throw new SFUnHandledException(ex);
            }
        }
        public void UpdateArticlesByArticleId(entArticles parEntArticles)
        {
            DbConnector insDbConnector = new DbConnector();
            try
            {

                insDbConnector.BeginTransaction();
                datArticles insDatArticles = new datArticles();

                insDatArticles.UpdateArticlesByArticleId(parEntArticles, insDbConnector);
                datArticleContents insDatArticleContents = new datArticleContents();

                datArticleContentPublishLocations insDatArticleContentPublishLocations = new datArticleContentPublishLocations();

                datArticleArticleCategories insDatArticleArticleCategories = new datArticleArticleCategories();
                entArticleArticleCategories insEntArticleArticleCategoriesMain = new entArticleArticleCategories();
                insEntArticleArticleCategoriesMain.ArticleId = parEntArticles.ArticleId;

                insDatArticleArticleCategories.DeleteArticleArticleCategoriesByArticleId(insEntArticleArticleCategoriesMain, insDbConnector);

                foreach (entArticleArticleCategories insEntArticleArticleCategories in parEntArticles.ArticleArticleCategories)
                {
                    insEntArticleArticleCategories.ArticleId = parEntArticles.ArticleId;
                    insDatArticleArticleCategories.InsertArticleArticleCategories(insEntArticleArticleCategories, insDbConnector);
                }

                foreach (entArticleContents insEntArticleContents in parEntArticles.ArticleContentsInsert)
                {
                    insEntArticleContents.ArticleId = parEntArticles.ArticleId;
                    
                    insDatArticleContents.InsertArticleContents(insEntArticleContents, insDbConnector);
                    foreach (entArticleContentPublishLocations insEntArticleContentPublishLocations in insEntArticleContents.ArticleContentPublishLocations)
                    {
                        insEntArticleContentPublishLocations.ArticleContentId = insEntArticleContents.ArticleContentId;
                        insDatArticleContentPublishLocations.InsertArticleContentPublishLocations(insEntArticleContentPublishLocations, insDbConnector);

                    }
                }
                foreach (entArticleContents insEntArticleContents in parEntArticles.ArticleContentsUpdate)
                {
                    insEntArticleContents.ArticleId = parEntArticles.ArticleId;
                    insDatArticleContents.UpdateArticleContentsByArticleContentId(insEntArticleContents, insDbConnector);

                    entArticleContentPublishLocations insEntArticleContentPublishLocationsDel = new entArticleContentPublishLocations();
                    insEntArticleContentPublishLocationsDel.ArticleContentId = insEntArticleContents.ArticleContentId;
                    insDatArticleContentPublishLocations.DeleteArticleContentPublishLocationsByArticleContentId(insEntArticleContentPublishLocationsDel, insDbConnector);
                    foreach (entArticleContentPublishLocations insEntArticleContentPublishLocations in insEntArticleContents.ArticleContentPublishLocations)
                    {
                        insEntArticleContentPublishLocations.ArticleContentId = insEntArticleContents.ArticleContentId;
                        insDatArticleContentPublishLocations.InsertArticleContentPublishLocations(insEntArticleContentPublishLocations, insDbConnector);

                    }
                }
                foreach (entArticleContents insEntArticleContents in parEntArticles.ArticleContentsDelete)
                {
                    entArticleContentPublishLocations insEntArticleContentPublishLocations = new entArticleContentPublishLocations();
                    insEntArticleContentPublishLocations.ArticleContentId = insEntArticleContents.ArticleContentId;
                    insDatArticleContentPublishLocations.DeleteArticleContentPublishLocationsByArticleContentId(insEntArticleContentPublishLocations, insDbConnector);
                    insDatArticleContents.DeleteArticleContentsByArticleContentId(insEntArticleContents, insDbConnector);
                }
                insDbConnector.CommitTransaction();
            }
            catch (SFException ex)
            {
                insDbConnector.RollbackTransaction();
                this.ErrorMessage = ex.Message;
            }
            catch (Exception ex)
            {
                insDbConnector.RollbackTransaction();
                throw new SFUnHandledException(ex);
            }
        }
        public void DeleteArticlesByArticleId(entArticles parEntArticles)
        {
            DbConnector insDbConnector = new DbConnector();
            insDbConnector.BeginTransaction();
            try
            {
                datArticleContents insDatArticleContents = new datArticleContents();
                entArticleContents insEntArticleContents = new entArticleContents();
                insEntArticleContents.ArticleId = parEntArticles.ArticleId;
                DataTable insDataTableArticleContents = insDatArticleContents.SelectArticleContentsByArticleId(insEntArticleContents, insDbConnector);

                datArticleComments insDatArticleComments = new datArticleComments();
                datArticleContentPublishLocations insDatArticleContentPublishLocations = new datArticleContentPublishLocations();
                foreach (DataRow dr in insDataTableArticleContents.Rows)
                {
                    if (dr["ArticleContentId"] != DBNull.Value)
                    {
                        entArticleContentPublishLocations insEntArticleContentPublishLocations = new entArticleContentPublishLocations();
                        insEntArticleContentPublishLocations.ArticleContentId = Convert.ToInt32(dr["ArticleContentId"]);
                        insDatArticleContentPublishLocations.DeleteArticleContentPublishLocationsByArticleContentId(insEntArticleContentPublishLocations, insDbConnector);


                        entArticleComments insEntArticleComments = new entArticleComments();
                        insEntArticleComments.ArticleContentId = Convert.ToInt32(dr["ArticleContentId"]);
                        insDatArticleComments.DeleteArticleCommentsByArticleContentId(insEntArticleComments, insDbConnector);

                        entArticleContents insEntArticleContents1 = new entArticleContents();
                        insEntArticleContents1.ArticleContentId = Convert.ToInt32(dr["ArticleContentId"]);

                        insDatArticleContents.DeleteArticleContentsByArticleContentId(insEntArticleContents1, insDbConnector);
                    }
                }

                datArticleArticleCategories insDatArticleArticleCategories = new datArticleArticleCategories();
                entArticleArticleCategories insEntArticleArticleCategories = new entArticleArticleCategories();
                insEntArticleArticleCategories.ArticleId = parEntArticles.ArticleId;
                insDatArticleArticleCategories.DeleteArticleArticleCategoriesByArticleId(insEntArticleArticleCategories, insDbConnector);




                datArticles insDatArticles = new datArticles();
                insDatArticles.DeleteArticlesByArticleId(parEntArticles, insDbConnector);

                insDbConnector.CommitTransaction();
            }
            catch (SFException ex)
            {
                insDbConnector.RollbackTransaction();
                this.ErrorMessage = ex.Message;
            }
            catch (Exception ex)
            {
                insDbConnector.RollbackTransaction();
                throw new SFUnHandledException(ex);
            }
        }
        public DataTable SelectArticlesArchive()
        {
            try
            {
                datArticles insDatArticles = new datArticles();
                DbConnector insDbConnector = new DbConnector();
                DataTable insDataTableArticlesArchive = insDatArticles.SelectArticlesArchive(insDbConnector);
                insDataTableArticlesArchive.Columns.Add("Value", typeof(string));
                insDataTableArticlesArchive.Columns.Add("Text", typeof(string));
                foreach (DataRow dr in insDataTableArticlesArchive.Rows)
                {
                    dr["Value"] = dr["ArticlesYear"].ToString() + (dr["ArticlesMonth"].ToString().Length == 1 ? "0" + dr["ArticlesMonth"].ToString() : dr["ArticlesMonth"].ToString());
                    dr["Text"] = GetErrorMessageMultilanguageString(30 + Convert.ToInt32(dr["ArticlesMonth"]), new object[] { }) + "," + dr["ArticlesYear"].ToString() + " (" + dr["ArticlesCount"].ToString() + ")";
                }
                return insDataTableArticlesArchive;
            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }

        public DataTable SelectArticlesRandom()
        {
            try
            {
                datArticles insDatArticles = new datArticles();
                DbConnector insDbConnector = new DbConnector();
                return insDatArticles.SelectArticlesRandom(insDbConnector);

            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
        public DataTable SelectArticlesByLanguageId()
        {
            try
            {
                datArticles insDatArticles = new datArticles();
                DbConnector insDbConnector = new DbConnector();
                return insDatArticles.SelectArticlesByLanguageId(insDbConnector);
            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
        public DataTable SelectArticlesByLanguageIdAndYearAndMonth(entArticles parEntArticles)
        {
            try
            {
                datArticles insDatArticles = new datArticles();
                DbConnector insDbConnector = new DbConnector();
                return insDatArticles.SelectArticlesByLanguageIdAndYearAndMonth(parEntArticles,insDbConnector);
            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
        public DataTable SelectArticlesByLanguageIdAndCategoryId(int categoryId)
        {
            try
            {
                datArticles insDatArticles = new datArticles();
                DbConnector insDbConnector = new DbConnector();
                return insDatArticles.SelectArticlesByLanguageIdAndCategoryId(categoryId, insDbConnector);
            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
        public DataTable SelectArticlesTopRead()
        {
            try
            {
                datArticles insDatArticles = new datArticles();
                DbConnector insDbConnector = new DbConnector();
                return insDatArticles.SelectArticlesTopRead(insDbConnector);

            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
        public DataTable SelectArticlesLatestMainPage()
        {
            try
            {
                datArticles insDatArticles = new datArticles();
                DbConnector insDbConnector = new DbConnector();
                return insDatArticles.SelectArticlesLatestMainPage(insDbConnector);

            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
        public DataTable SelectArticlesByDateMainPage(entArticles parEntArticles)
        {
            try
            {
                datArticles insDatArticles = new datArticles();
                DbConnector insDbConnector = new DbConnector();
                return insDatArticles.SelectArticlesByDateMainPage(parEntArticles,insDbConnector);

            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
    }
}